﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace SharedGamesClasses {
    
    /// <summary>
    /// TwoUp.cs represents game play of Two Up 
    /// with the user matched against the computer
    /// </summary>
    public static class TwoUp {

        private const string HEADS = "\nYou threw Heads - you win\n";
        private const string ODDS = "\nYou threw Odds\n";
        private const string TAILS = "\nYou threw Tails - I win\n";

        private const string PLAY_AGAIN = "\n\nPlay again? (Y or N):";
        private const string THROW_COINS = "\nPress any key to throw coins again.";
        private const string PLAY = "1. Two-up \n\n2. Not yet available \n\n0. Cancel Selcetion!";

        private static Coin coin1;
        private static Coin coin2;
        private static int playerPoint = 0;
        private static int computerPoint = 0;

        //************* more class variables needed **************************

        /// <summary>
        /// Since the class is static, we need to be able to initialise local coins.
        /// </summary>
        public static void SetUpCoins() {
            coin1 = new Coin();
            coin2 = new Coin();
        } //end SetUpCoins


        /// <summary>
        /// Reset all class variables to "zeroed" values,
        /// counters and flags but not the coins.
        /// 
        /// used by both Console and GUI to ensure subsequent 
        /// playing of the game is correct
        /// </summary>
        public static void ResetGlobals(){
            // Code needed.
            coin1 = new Coin();
            coin2 = new Coin();
            playerPoint = 0;
            computerPoint = 0;
          
        }//end ResetGlobals


        /// <summary>
        /// Plays the game of Two Up until the user decides to stop
        /// Used by Console version only
        /// </summary>
        public static void PlayConsole() {
            // Code needed.

            // Before exiting the game, reset class variables
            // to appropriate starting values.
            System.Console.WriteLine(PLAY);
            int choose;
            char playAgain = 'y';
            choose = int.Parse(Console.In.ReadLine());
            while (choose == 2)
            {
                System.Console.WriteLine(PLAY);   
                choose = int.Parse(Console.In.ReadLine());
            }

            if (choose == 0)
            {
                ResetGlobals();
                return;
            }
            if (choose == 1)
            {
                while(playAgain == 'y')
                {
                    SetUpCoins();
                    ThrowCoins();
                    int isPlayerWin = OutputWinner();

                    if (isPlayerWin == 2)
                    {
                        Console.Out.WriteLine(HEADS);
                        playerPoint++;
                        Console.Out.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
                        Console.Out.WriteLine(PLAY_AGAIN);

                        playAgain = char.Parse(Console.In.ReadLine());

                    }
                    if (isPlayerWin == 1)
                    {
                        Console.Out.WriteLine(ODDS);

                        Console.Out.WriteLine(THROW_COINS);
                        Console.ReadKey();
                        coin1.Flip();
                        if (IsHeads(1))
                        {
                            Console.Out.WriteLine(HEADS);
                            playerPoint++;
                            Console.Out.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
                            Console.Out.WriteLine(PLAY_AGAIN);

                            playAgain = char.Parse(Console.In.ReadLine());
                        }
                        else
                        {
                            Console.Out.WriteLine(TAILS);
                            computerPoint++;
                            Console.Out.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
                            Console.Out.WriteLine(PLAY_AGAIN);
                            playAgain = char.Parse(Console.In.ReadLine());
                        }
                    }
                    if (isPlayerWin == 0)
                    {
                        Console.Out.WriteLine(TAILS);
                        computerPoint++;
                        Console.Out.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
                        Console.Out.WriteLine(PLAY_AGAIN);
                        playAgain = char.Parse(Console.In.ReadLine());
                    }
                }
                if (playAgain == 'n')
                {
                    ResetGlobals();
                    return;
                }
            }
            ResetGlobals();
        }//end PlayConsole

        public static int Play()
        {
            SetUpCoins();
            ThrowCoins();
            int isPlayerWin = OutputWinner();

            if (isPlayerWin == 2)
            {
                Trace.WriteLine(HEADS);
                playerPoint += 1;
                Trace.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
            }
            if (isPlayerWin == 1)
            {
                Trace.WriteLine(ODDS);
                Trace.WriteLine("Click Throw Coins");
            }
            if (isPlayerWin == 0)
            {
                Trace.WriteLine(TAILS);
                computerPoint += 1;
                Trace.WriteLine("You have " + playerPoint.ToString() + " and I have " + computerPoint.ToString() + " point");
            }
            return isPlayerWin;
        }

        /// <summary>
        /// Flips both coins
        /// Pre: Local coins 1 and 2 exist
        /// Post: coin1 and coin2 flipped.
        /// </summary>
        public static void ThrowCoins() {
            coin1.Flip();
            coin2.Flip();
        }// end ThrowCoins

        /// <summary>
        /// Determines the winner of the current throw if one exists.
        /// Pre: true
        /// Post: updates class variables appropriately and displays the scorecard if a winner is found.
        /// </summary>
        /// <returns>The number of heads from the current throw: 0, 1, or 2.</returns>
        public static int OutputWinner() {
            int numberOfHeads = 0;
            if (IsHeads(1))
                numberOfHeads++;
            if (IsHeads(2))
                numberOfHeads++;
            return numberOfHeads;
        }// end OutputWinner

        /// <summary>
        /// Needed for GUI (only).
        /// To display the correct face of a coin
        /// </summary>
        /// <param name="coinNum"> which Coin</param>
        /// <returns> true if face is Heads</returns>
        public static bool IsHeads(int coinNum) {
            if (coinNum == 1)
                return coin1.IsHeads();
            else
                return coin2.IsHeads();
        }

    }//end class TwoUp
}//end namespace

